package com.omega_r.omegarecyclerview.expandable_example.support_sticky;

import com.omega_r.libs.omegarecyclerview.base.BaseBean;
import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import com.omega_r.omegarecyclerview.expandable_example.GetData;
import com.omega_r.omegarecyclerview.expandable_example.core.SimpleData;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;

public class DefaultStickyExpandableAbility extends Ability implements AbsButton.CheckedStateChangedListener {
    private List<SimpleData> items;
    private BaseProvider<SimpleData> provider;
    private DefaultStickyViewHeadHolder defaultStickyViewHeadHolder;
    private EventHandler handler;
    private Text textToast;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_expandable);
        handler = new EventHandler(EventRunner.getMainEventRunner());
        createItems();
        initView();
    }

    private void createItems() {
        items = GetData.getData(this);
        SimpleData simpleData5 = SimpleData.from(0, null, 1500,
                null);
        SimpleData simpleData6 = SimpleData.from(0, null, 1914,
                null);
        items.add(0, simpleData5);
        items.add(3, simpleData6);
    }

    private void initView() {
        textToast = (Text) findComponentById(ResourceTable.Id_text_toast);
        RadioButton radioButtonFade = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_fade);
        RadioButton radioButtonDropdown = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_dropdown);
        radioButtonDropdown.setChecked(true);
        RadioButton radioButtonSingle = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_single);
        radioButtonSingle.setChecked(true);
        RadioButton radioButtonMultiple = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_multiple);
        radioButtonFade.setCheckedStateChangedListener(this);
        radioButtonDropdown.setCheckedStateChangedListener(this);
        radioButtonMultiple.setCheckedStateChangedListener(this);
        radioButtonSingle.setCheckedStateChangedListener(this);
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_recyclerview_core_expand);
        provider = new BaseProvider(getContext(), listContainer, items, ResourceTable.Layout_item_core_adapter_header);
        provider.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                defaultStickyViewHeadHolder = new DefaultStickyViewHeadHolder(item, listContainer);
                return defaultStickyViewHeadHolder;
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((DefaultStickyViewHeadHolder) baseViewHolder).updateView((SimpleData) object, radioButtonSingle.isChecked(),
                        radioButtonDropdown.isChecked(), position, new BaseViewHolder.OnClickListener() {
                            @Override
                            public void onCollapse(BaseBean bean, int position) {
                                show(bean, position, "onCollapse ");
                            }

                            @Override
                            public void onExpand(BaseBean bean, int position) {
                                show(bean, position, "onExpand ");
                            }
                        });
            }
        });
        provider.setStickyListener(object -> ((SimpleData) object).getItemType() == 1 ? true : false);
        Button btnTestUpdateChild = (Button) findComponentById(ResourceTable.Id_button_test_update_child);
        btnTestUpdateChild.setClickedListener(component -> {
            if (!items.get(5).getmQuotes().get(1).equalsIgnoreCase("UPDATED QUOTE")) {
                List<SimpleData> simpleData = new ArrayList<>(items);
                List<String> item = simpleData.get(5).getmQuotes();
                item.add(1, "UPDATED QUOTE");
            }
            provider.notifyDataSetItemChanged(5);
        });
        listContainer.setItemProvider(provider);
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean b) {
        switch (absButton.getId()) {
            case ResourceTable.Id_radiobutton_fade:
            case ResourceTable.Id_radiobutton_single:
                provider.notifyDataChanged();
                break;
            default:
                break;
        }
    }

    private void show(BaseBean baseBean, int position, String msg) {
        int tempPosition = 0;
        if (((SimpleData) baseBean).getmYear() == 1914) {
            tempPosition = position - 2;
        } else if (((SimpleData) baseBean).getmYear() == 1500) {
            tempPosition = position - 1;
        }
        textToast.setVisibility(Component.VISIBLE);
        textToast.setText(msg + tempPosition);
        handler.postTask(() -> textToast.setVisibility(Component.HIDE), 1500);
    }
}
